package com.ruoyi.eqmonitor.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.eqmonitor.dispose.service.IExteriorSiteAlarmService;
import com.ruoyi.eqmonitor.domain.OptHandleFlowRecord;
import com.ruoyi.eqmonitor.domain.OptPatrolEvent;
import com.ruoyi.eqmonitor.domain.OptPatrolTask;
import com.ruoyi.eqmonitor.mapper.OptPatrolTaskMapper;
import com.ruoyi.eqmonitor.service.*;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtvOutletProblemService;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtvOverflowWellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 巡检任务Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-05
 */
@Service
public class OptPatrolTaskServiceImpl extends ServiceImpl<OptPatrolTaskMapper, OptPatrolTask> implements IOptPatrolTaskService {

    @Autowired
    private OptPatrolTaskMapper optPatrolTaskMapper;

    @Autowired
    private IOptHandleFlowRecordService optHandleFlowRecordService;

    @Autowired
    private IOptPatrolEventService optPatrolEventService;

    @Autowired
    private IOptDeviceAlarmService iOptDeviceAlarmService;

    @Resource
    private IExteriorSiteAlarmService exteriorSiteAlarmService;

    @Resource
    private ISmdtvOutletProblemService smdtvOutletProblemService;
    @Resource
    private ISmdtvOverflowWellService smdtvOverflowWellService;

    @Override
    public OptPatrolTask selectOptPatrolTaskById(Long id) {
        return optPatrolTaskMapper.selectOptPatrolTaskById(id);
    }

    @Override
    public List<OptPatrolTask> selectOptPatrolTaskList(OptPatrolTask optPatrolTask) {
        return optPatrolTaskMapper.selectOptPatrolTaskList(optPatrolTask);
    }

    @Override
    public boolean add(OptPatrolTask optPatrolTask) {
        try {
            if (optPatrolTask.getTypeId() == 1) {

                //插入任务表
                optPatrolTask.setStatusId(2L);
                optPatrolTask.setStatusName("待确认");
                optPatrolTask.setCreateBy(SecurityUtils.getUsername());
                optPatrolTask.setCreateTime(new Date());
                boolean isSuccess = save(optPatrolTask);

                //插入流程表记录——任务指派
                OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
                optHandleFlowRecord.setTaskId(optPatrolTask.getId());//当前任务Id
                optHandleFlowRecord.setActivityName("指派");
                optHandleFlowRecord.setActivityFlag(1L);
                optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
                optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
                optHandleFlowRecord.setHandleTime(new Date());
                optHandleFlowRecord.setAssignContent("指派给—" + optPatrolTask.getReceiveUserName() + ",请及时处理");
                optHandleFlowRecord.setContent("系统自动指派");
                optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
                Date createDate = new Date();
                createDate.setTime(createDate.getTime() + 10);
                optHandleFlowRecord.setCreateTime(createDate);
                optHandleFlowRecordService.save(optHandleFlowRecord);

                //插入流程表记录——任务待确认
                OptHandleFlowRecord optHandleFlowRecordConfirm = new OptHandleFlowRecord();
                optHandleFlowRecordConfirm.setTaskId(optPatrolTask.getId());//当前任务Id
                optHandleFlowRecordConfirm.setActivityName("待确认");
                optHandleFlowRecordConfirm.setActivityFlag(2L);
                optHandleFlowRecordConfirm.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
                optHandleFlowRecordConfirm.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
                optHandleFlowRecordConfirm.setHandleTime(new Date());
                optHandleFlowRecordConfirm.setContent("系统自动");
                optHandleFlowRecordConfirm.setCreateBy(SecurityUtils.getUsername());
                createDate.setTime(createDate.getTime() + 20);
                optHandleFlowRecordConfirm.setCreateTime(createDate);
                optHandleFlowRecordService.save(optHandleFlowRecordConfirm);

                //插入事件表
                OptPatrolEvent optPatrolEvent = new OptPatrolEvent();
                optPatrolEvent.setTaskId(optPatrolTask.getId());//当前任务Id
                optPatrolEvent.setName("设备自动报警");

                //按日期生成事件编码
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                String evtCode = sdf.format(date);
                optPatrolEvent.setCode("TZ" + evtCode);

                optPatrolEvent.setTargetId(optPatrolTask.getTargetId());
                optPatrolEvent.setTargetName(optPatrolTask.getTargetName());
                optPatrolEvent.setSourceId(2L);
                optPatrolEvent.setSourceName("设备报警");
                optPatrolEvent.setTypeId(optPatrolTask.getTypeId());//对应事件类型Id——设备报警类型Id
                optPatrolEvent.setTypeName(optPatrolTask.getTypeName());//对应事件类型名称——设备报警类型名称
                optPatrolEvent.setLevelId(1L);
                optPatrolEvent.setLevelName("一般");
                optPatrolEvent.setIncidentTime(new Date());
                optPatrolEvent.setReportPersonId(SecurityUtils.getUserId());//当前登录用户Id
                optPatrolEvent.setReportPersonName(SecurityUtils.getUsername());//当前登录用户姓名
                optPatrolEvent.setReportTime(new Date());
                optPatrolEvent.setDeviceCode(optPatrolTask.getDeviceCode());//设备编码
                optPatrolEvent.setDeviceName(optPatrolTask.getDeviceName());//设备名称
                optPatrolEvent.setDeviceTypeId(optPatrolTask.getDeviceTypeId());
                optPatrolEvent.setDeviceTypeName(optPatrolTask.getDeviceTypeName());
                optPatrolEvent.setDeviceAddress(optPatrolTask.getDeviceAddress());
                optPatrolEvent.setAddress(optPatrolTask.getAddress());
                optPatrolEvent.setX(optPatrolTask.getX());
                optPatrolEvent.setY(optPatrolTask.getY());
                optPatrolEvent.setLng(optPatrolTask.getLng());
                optPatrolEvent.setLat(optPatrolTask.getLat());
                optPatrolEvent.setContent(optPatrolTask.getRemark());//任务描述，设备报警的内容
                optPatrolEvent.setAlarmId(optPatrolTask.getAlarmId());//设备报警Id
                //默认到待办
                optPatrolEvent.setStatus(10L);//事件直接到待办
                //判断处置单位是否为空
                if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId())) {
                    optPatrolEvent.setStatus(11L);//事件直接事件受理
                }
                //判断处置单位任务接收人是否为空,有处置单位和任务接收人则到派遣人员
                if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId()) && optPatrolTask.getReceiveUserId() > 0) {
                    optPatrolEvent.setStatus(12L);//事件直接到派遣人员
                }
                optPatrolEvent.setCreateBy(SecurityUtils.getUsername());
                optPatrolEvent.setCreateTime(new Date());
                optPatrolEvent.setObjectId(optPatrolTask.getReceiveUserId());
                optPatrolEvent.setObjectName(optPatrolTask.getReceiveUserName());
                optPatrolEvent.setDisposeDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));//处置部门Id
                optPatrolEvent.setDisposeDeptName(optPatrolTask.getExecuteOrganizationName());//处置部门名称
                optPatrolEvent.setDisposeShouldFinishTime(optPatrolTask.getEndTime());//处置时限时间——应完成时间
                optPatrolEventService.save(optPatrolEvent);

                //插入流程表记录——事件上报
                OptHandleFlowRecord optHandleFlowRecordEvent = new OptHandleFlowRecord();
                //optHandleFlowRecordEvent.setTaskId(optPatrolTask.getId());//当前任务Id
                optHandleFlowRecordEvent.setBusinessId(optPatrolEvent.getId());//当前事件Id
                optHandleFlowRecordEvent.setActivityName("事件上报");
                optHandleFlowRecordEvent.setActivityFlag(10L);
                //optHandleFlowRecordEvent.setHandleUserId(optPatrolTask.getReceiveUserId());//当前登录用户Id
                //optHandleFlowRecordEvent.setHandleUserName(optPatrolTask.getReceiveUserName());//当前登录用户姓名
                //判断处置单位是否为空
                if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId())) {
                    optHandleFlowRecordEvent.setHandleDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));
                    optHandleFlowRecordEvent.setHandleDeptName(optPatrolTask.getExecuteOrganizationName());
                }
                optHandleFlowRecordEvent.setShouldFinishTime(optPatrolTask.getEndTime());//应完成时间
                optHandleFlowRecordEvent.setHandleTimeLimit(optPatrolTask.getEndTime());//处置时限截止时间
                //optHandleFlowRecordEvent.setHandleTime(new Date());
                optHandleFlowRecordEvent.setCreateBy(SecurityUtils.getUsername());//当前登录用户姓名
                createDate.setTime(createDate.getTime() + 30);
                optHandleFlowRecordEvent.setCreateTime(createDate);
                optHandleFlowRecordEvent.setContent(optPatrolTask.getRemark());
                optHandleFlowRecordService.save(optHandleFlowRecordEvent);

                //判断处置单位是否为空,有处置单位则到事件受理
                if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId())) {
                    //插入流程表记录——事件受理
                    OptHandleFlowRecord optHandleFlowRecordEventAccept = new OptHandleFlowRecord();
                    //optHandleFlowRecordEvent.setTaskId(optPatrolTask.getId());//当前任务Id
                    optHandleFlowRecordEventAccept.setBusinessId(optPatrolEvent.getId());//当前事件Id
                    optHandleFlowRecordEventAccept.setActivityName("事件受理");
                    optHandleFlowRecordEventAccept.setActivityFlag(11L);
                    //optHandleFlowRecordEvent.setHandleUserId(optPatrolTask.getReceiveUserId());//当前登录用户Id
                    //optHandleFlowRecordEvent.setHandleUserName(optPatrolTask.getReceiveUserName());//当前登录用户姓名
                    optHandleFlowRecordEventAccept.setHandleDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));
                    optHandleFlowRecordEventAccept.setHandleDeptName(optPatrolTask.getExecuteOrganizationName());
                    optHandleFlowRecordEventAccept.setShouldFinishTime(optPatrolTask.getEndTime());//应完成时间
                    optHandleFlowRecordEventAccept.setHandleTimeLimit(optPatrolTask.getEndTime());//处置时限截止时间
                    //optHandleFlowRecordEvent.setHandleTime(new Date());
                    optHandleFlowRecordEventAccept.setCreateBy(SecurityUtils.getUsername());//当前登录用户姓名
                    createDate.setTime(createDate.getTime() + 40);
                    optHandleFlowRecordEventAccept.setCreateTime(createDate);
                    optHandleFlowRecordEventAccept.setContent("系统自动受理");
                    optHandleFlowRecordService.save(optHandleFlowRecordEventAccept);
                }

                //判断处置单位任务接收人是否为空,有处置单位和任务接收人则到派遣人员
                if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId()) && optPatrolTask.getReceiveUserId() > 0) {
                    //插入流程表记录——事件受理
                    OptHandleFlowRecord optHandleFlowRecordEventAccept = new OptHandleFlowRecord();
                    //optHandleFlowRecordEvent.setTaskId(optPatrolTask.getId());//当前任务Id
                    optHandleFlowRecordEventAccept.setBusinessId(optPatrolEvent.getId());//当前事件Id
                    optHandleFlowRecordEventAccept.setActivityName("派遣人员");
                    optHandleFlowRecordEventAccept.setActivityFlag(12L);
                    optHandleFlowRecordEventAccept.setHandleUserId(optPatrolTask.getReceiveUserId());//当前登录用户Id
                    optHandleFlowRecordEventAccept.setHandleUserName(optPatrolTask.getReceiveUserName());//当前登录用户姓名
                    optHandleFlowRecordEventAccept.setHandleDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));
                    optHandleFlowRecordEventAccept.setHandleDeptName(optPatrolTask.getExecuteOrganizationName());
                    optHandleFlowRecordEventAccept.setShouldFinishTime(optPatrolTask.getEndTime());//应完成时间
                    optHandleFlowRecordEventAccept.setHandleTimeLimit(optPatrolTask.getEndTime());//处置时限截止时间
                    //optHandleFlowRecordEvent.setHandleTime(new Date());
                    optHandleFlowRecordEventAccept.setCreateBy(SecurityUtils.getUsername());//当前登录用户姓名
                    createDate.setTime(createDate.getTime() + 50);
                    optHandleFlowRecordEventAccept.setCreateTime(createDate);
                    optHandleFlowRecordEventAccept.setContent("系统自动派遣");
                    optHandleFlowRecordService.save(optHandleFlowRecordEventAccept);
                }

                return isSuccess;
            } else {

                //插入任务表
                optPatrolTask.setStatusId(2L);
                optPatrolTask.setStatusName("待确认");
                optPatrolTask.setCreateTime(new Date());
                optPatrolTask.setCreateBy(SecurityUtils.getUsername());

                boolean isSuccess = save(optPatrolTask);

                //插入流程表记录——任务指派
                OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
                optHandleFlowRecord.setTaskId(optPatrolTask.getId());//当前任务Id
                optHandleFlowRecord.setActivityName("指派");
                optHandleFlowRecord.setActivityFlag(1L);
                optHandleFlowRecord.setHandleUserId(optPatrolTask.getReceiveUserId());//任务接收人Id
                optHandleFlowRecord.setHandleUserName(optPatrolTask.getReceiveUserName());//任务接收人姓名
                optHandleFlowRecord.setHandleTime(new Date());
                optHandleFlowRecord.setAssignContent(optPatrolTask.getTypeName() + ",指派给—" + optPatrolTask.getReceiveUserName() + ",请及时处理");
                optHandleFlowRecord.setContent(optPatrolTask.getRemark());
                optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
                Date createDate = new Date();
                createDate.setTime(createDate.getTime() + 10);
                optHandleFlowRecord.setCreateTime(createDate);
                optHandleFlowRecordService.save(optHandleFlowRecord);

                //插入流程表记录——任务待确认
                OptHandleFlowRecord optHandleFlowRecordConfirm = new OptHandleFlowRecord();
                optHandleFlowRecordConfirm.setTaskId(optPatrolTask.getId());//当前任务Id
                optHandleFlowRecordConfirm.setActivityName("待确认");
                optHandleFlowRecordConfirm.setActivityFlag(2L);
                optHandleFlowRecordConfirm.setHandleUserId(optPatrolTask.getReceiveUserId());//任务接收人Id
                optHandleFlowRecordConfirm.setHandleUserName(optPatrolTask.getReceiveUserName());//任务接收人姓名
                optHandleFlowRecordConfirm.setHandleTime(new Date());
                optHandleFlowRecordConfirm.setContent(optPatrolTask.getTypeName() + ",已指派给—" + optPatrolTask.getReceiveUserName() + ",待确认");
                optHandleFlowRecordConfirm.setCreateBy(SecurityUtils.getUsername());
                createDate.setTime(createDate.getTime() + 20);
                optHandleFlowRecordConfirm.setCreateTime(createDate);
                optHandleFlowRecordService.save(optHandleFlowRecordConfirm);
                return isSuccess;
            }
        } catch (Exception ex) {
            log.error("巡检任务新增传入参数对象异常记录：" + com.alibaba.fastjson.JSONObject.toJSONString(optPatrolTask));
            log.error("巡检任务新增add失败：" + ex.getMessage());
            return false;
        }
    }

    @Override
    public boolean confirm(OptPatrolTask optPatrolTask) {
        //插入流程表记录——任务确认
        OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
        optHandleFlowRecord.setTaskId(optPatrolTask.getId());//当前任务Id
        //optHandleFlowRecord.setBusinessId(optPatrolRecord.getId());
        optHandleFlowRecord.setActivityName("确认");
        optHandleFlowRecord.setActivityFlag(3L);
        optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
        optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
        optHandleFlowRecord.setHandleTime(new Date());
        optHandleFlowRecord.setCreateTime(new Date());
        //optHandleFlowRecord.setAssignContent("已确认，处置中");
        optHandleFlowRecord.setContent("任务已确认");
        optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
        optHandleFlowRecordService.save(optHandleFlowRecord);

        OptPatrolTask task = getById(optPatrolTask.getId());
        task.setStatusId(3L);
        task.setStatusName("确认");
        task.setUpdateTime(new Date());
        task.setUpdateBy(SecurityUtils.getUsername());
        return updateById(task);
    }

    @Override
    public boolean finish(OptPatrolTask optPatrolTask) {
        //插入流程表记录——任务清除(完成)
        OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
        optHandleFlowRecord.setTaskId(optPatrolTask.getId());//当前任务Id
        //optHandleFlowRecord.setBusinessId(optPatrolRecord.getId());
        optHandleFlowRecord.setActivityName("清除");
        optHandleFlowRecord.setActivityFlag(5L);
        optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
        optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
        optHandleFlowRecord.setHandleTime(new Date());
        optHandleFlowRecord.setCreateTime(new Date());
        //optHandleFlowRecord.setAssignContent("已确认，处置中");
        optHandleFlowRecord.setContent("任务已完成，清除");
        optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
        optHandleFlowRecordService.save(optHandleFlowRecord);

        OptPatrolTask task = getById(optPatrolTask.getId());
        task.setStatusId(5L);
        task.setStatusName("清除");
        task.setUpdateTime(new Date());
        task.setUpdateBy(SecurityUtils.getUsername());
        return updateById(task);
    }

    @Override
    public boolean deviceTaskAdd(OptPatrolTask optPatrolTask) {
        try {
            //插入任务表
            if (ObjectUtil.isNotNull(optPatrolTask.getMinorId()) && optPatrolTask.getState() != null && (optPatrolTask.getState() == 0 || optPatrolTask.getState() == 1)){
                optPatrolTask.setId(null);
            }
            optPatrolTask.setStatusId(2L);
            optPatrolTask.setStatusName("待确认");
            optPatrolTask.setCreateBy(SecurityUtils.getUsername());
            optPatrolTask.setCreateTime(new Date());
            boolean isSuccess = save(optPatrolTask);

            //插入流程表记录——任务指派
            OptHandleFlowRecord optHandleFlowRecord = new OptHandleFlowRecord();
            optHandleFlowRecord.setTaskId(optPatrolTask.getId());//当前任务Id
            optHandleFlowRecord.setActivityName("指派");
            optHandleFlowRecord.setActivityFlag(1L);
            optHandleFlowRecord.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
            optHandleFlowRecord.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
            optHandleFlowRecord.setHandleTime(new Date());
            optHandleFlowRecord.setAssignContent("指派给—" + optPatrolTask.getReceiveUserName() + ",请及时处理");
            optHandleFlowRecord.setContent("系统自动指派");
            optHandleFlowRecord.setCreateBy(SecurityUtils.getUsername());
            Date createDate = new Date();
            createDate.setTime(createDate.getTime() + 10);
            optHandleFlowRecord.setCreateTime(createDate);
            optHandleFlowRecordService.save(optHandleFlowRecord);

            //插入流程表记录——任务待确认
            OptHandleFlowRecord optHandleFlowRecordConfirm = new OptHandleFlowRecord();
            optHandleFlowRecordConfirm.setTaskId(optPatrolTask.getId());//当前任务Id
            optHandleFlowRecordConfirm.setActivityName("待确认");
            optHandleFlowRecordConfirm.setActivityFlag(2L);
            optHandleFlowRecordConfirm.setHandleUserId(SecurityUtils.getUserId());//当前登录用户Id
            optHandleFlowRecordConfirm.setHandleUserName(SecurityUtils.getUsername());//当前登录用户姓名
            optHandleFlowRecordConfirm.setHandleTime(new Date());
            optHandleFlowRecordConfirm.setContent("系统自动");
            optHandleFlowRecordConfirm.setCreateBy(SecurityUtils.getUsername());
            createDate.setTime(createDate.getTime() + 20);
            optHandleFlowRecordConfirm.setCreateTime(createDate);
            optHandleFlowRecordService.save(optHandleFlowRecordConfirm);

            //插入事件表
            OptPatrolEvent optPatrolEvent = new OptPatrolEvent();
            optPatrolEvent.setTaskId(optPatrolTask.getId());//当前任务Id
            optPatrolEvent.setName("设备自动报警");

            //按日期生成事件编码
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String evtCode = sdf.format(date);
            optPatrolEvent.setCode("TZ" + evtCode);

            optPatrolEvent.setTargetId(optPatrolTask.getTargetId());
            optPatrolEvent.setTargetName(optPatrolTask.getTargetName());
            optPatrolEvent.setSourceId(2L);
            optPatrolEvent.setSourceName("设备报警");
            optPatrolEvent.setTypeId(optPatrolTask.getTypeId());//对应事件类型Id——设备报警类型Id
            optPatrolEvent.setTypeName(optPatrolTask.getTypeName());//对应事件类型名称——设备报警类型名称
            optPatrolEvent.setLevelId(1L);
            optPatrolEvent.setLevelName("一般");
            optPatrolEvent.setIncidentTime(new Date());
            optPatrolEvent.setReportPersonId(SecurityUtils.getUserId());//当前登录用户Id
            optPatrolEvent.setReportPersonName(SecurityUtils.getUsername());//当前登录用户姓名
            optPatrolEvent.setReportTime(new Date());
            optPatrolEvent.setDeviceCode(optPatrolTask.getDeviceCode());//设备编码
            optPatrolEvent.setDeviceName(optPatrolTask.getDeviceName());//设备名称
            optPatrolEvent.setDeviceTypeId(optPatrolTask.getDeviceTypeId());
            optPatrolEvent.setDeviceTypeName(optPatrolTask.getDeviceTypeName());
            optPatrolEvent.setDeviceAddress(optPatrolTask.getDeviceAddress());
            optPatrolEvent.setAddress(optPatrolTask.getAddress());
            optPatrolEvent.setX(optPatrolTask.getX());
            optPatrolEvent.setY(optPatrolTask.getY());
            optPatrolEvent.setLng(optPatrolTask.getLng());
            optPatrolEvent.setLat(optPatrolTask.getLat());
            optPatrolEvent.setContent(optPatrolTask.getRemark());//任务描述，设备报警的内容
            optPatrolEvent.setAlarmId(optPatrolTask.getAlarmId());//设备报警Id
            Integer state = optPatrolTask.getState();
            String minorId = optPatrolTask.getMinorId();
            if (ObjectUtil.isNotNull(minorId) && state != null){
                if (state == 0) {
                    optPatrolEvent.setMinorId("排口"+minorId);
                } else if (state == 1) {
                    optPatrolEvent.setMinorId("溢流井"+minorId);
                } else if (state == 2) {
                    optPatrolEvent.setMinorId("断面"+minorId);
                }
            }

            //默认到待办
            optPatrolEvent.setStatus(10L);//事件直接到待办
            //判断处置单位是否为空
            if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId())) {
                optPatrolEvent.setStatus(11L);//事件直接事件受理
            }
            //判断处置单位任务接收人是否为空,有处置单位和任务接收人则到派遣人员
            if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId()) && optPatrolTask.getReceiveUserId() > 0) {
                optPatrolEvent.setStatus(12L);//事件直接到派遣人员
            }
            optPatrolEvent.setCreateBy(SecurityUtils.getUsername());
            optPatrolEvent.setCreateTime(new Date());
            optPatrolEvent.setObjectId(optPatrolTask.getReceiveUserId());
            optPatrolEvent.setObjectName(optPatrolTask.getReceiveUserName());
            optPatrolEvent.setDisposeDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));//处置部门Id
            optPatrolEvent.setDisposeDeptName(optPatrolTask.getExecuteOrganizationName());//处置部门名称
            optPatrolEvent.setDisposeShouldFinishTime(optPatrolTask.getEndTime());//处置时限时间——应完成时间
            optPatrolEventService.save(optPatrolEvent);

            //插入流程表记录——事件上报
            OptHandleFlowRecord optHandleFlowRecordEvent = new OptHandleFlowRecord();
            //optHandleFlowRecordEvent.setTaskId(optPatrolTask.getId());//当前任务Id
            optHandleFlowRecordEvent.setBusinessId(optPatrolEvent.getId());//当前事件Id
            optHandleFlowRecordEvent.setActivityName("事件上报");
            optHandleFlowRecordEvent.setActivityFlag(10L);
            //optHandleFlowRecordEvent.setHandleUserId(optPatrolTask.getReceiveUserId());//当前登录用户Id
            //optHandleFlowRecordEvent.setHandleUserName(optPatrolTask.getReceiveUserName());//当前登录用户姓名
            //判断处置单位是否为空
            if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId())) {
                optHandleFlowRecordEvent.setHandleDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));
                optHandleFlowRecordEvent.setHandleDeptName(optPatrolTask.getExecuteOrganizationName());
            }
            optHandleFlowRecordEvent.setShouldFinishTime(optPatrolTask.getEndTime());//应完成时间
            optHandleFlowRecordEvent.setHandleTimeLimit(optPatrolTask.getEndTime());//处置时限截止时间
            //optHandleFlowRecordEvent.setHandleTime(new Date());
            optHandleFlowRecordEvent.setCreateBy(SecurityUtils.getUsername());//当前登录用户姓名
            createDate.setTime(createDate.getTime() + 30);
            optHandleFlowRecordEvent.setCreateTime(createDate);
            optHandleFlowRecordEvent.setContent(optPatrolTask.getRemark());
            optHandleFlowRecordService.save(optHandleFlowRecordEvent);

            //判断处置单位是否为空,有处置单位则到事件受理
            if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId())) {
                //插入流程表记录——事件受理
                OptHandleFlowRecord optHandleFlowRecordEventAccept = new OptHandleFlowRecord();
                //optHandleFlowRecordEvent.setTaskId(optPatrolTask.getId());//当前任务Id
                optHandleFlowRecordEventAccept.setBusinessId(optPatrolEvent.getId());//当前事件Id
                optHandleFlowRecordEventAccept.setActivityName("事件受理");
                optHandleFlowRecordEventAccept.setActivityFlag(11L);
                //optHandleFlowRecordEvent.setHandleUserId(optPatrolTask.getReceiveUserId());//当前登录用户Id
                //optHandleFlowRecordEvent.setHandleUserName(optPatrolTask.getReceiveUserName());//当前登录用户姓名
                optHandleFlowRecordEventAccept.setHandleDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));
                optHandleFlowRecordEventAccept.setHandleDeptName(optPatrolTask.getExecuteOrganizationName());
                optHandleFlowRecordEventAccept.setShouldFinishTime(optPatrolTask.getEndTime());//应完成时间
                optHandleFlowRecordEventAccept.setHandleTimeLimit(optPatrolTask.getEndTime());//处置时限截止时间
                //optHandleFlowRecordEvent.setHandleTime(new Date());
                optHandleFlowRecordEventAccept.setCreateBy(SecurityUtils.getUsername());//当前登录用户姓名
                createDate.setTime(createDate.getTime() + 40);
                optHandleFlowRecordEventAccept.setCreateTime(createDate);
                optHandleFlowRecordEventAccept.setContent("系统自动受理");
                optHandleFlowRecordService.save(optHandleFlowRecordEventAccept);
            }

            //判断处置单位任务接收人是否为空,有处置单位和任务接收人则到派遣人员
            if (StringUtils.isNotEmpty(optPatrolTask.getExecuteOrganizationId()) && optPatrolTask.getReceiveUserId() > 0) {
                //插入流程表记录——事件受理
                OptHandleFlowRecord optHandleFlowRecordEventAccept = new OptHandleFlowRecord();
                //optHandleFlowRecordEvent.setTaskId(optPatrolTask.getId());//当前任务Id
                optHandleFlowRecordEventAccept.setBusinessId(optPatrolEvent.getId());//当前事件Id
                optHandleFlowRecordEventAccept.setActivityName("派遣人员");
                optHandleFlowRecordEventAccept.setActivityFlag(12L);
                optHandleFlowRecordEventAccept.setHandleUserId(optPatrolTask.getReceiveUserId());//当前登录用户Id
                optHandleFlowRecordEventAccept.setHandleUserName(optPatrolTask.getReceiveUserName());//当前登录用户姓名
                optHandleFlowRecordEventAccept.setHandleDeptId(Long.parseLong(optPatrolTask.getExecuteOrganizationId()));
                optHandleFlowRecordEventAccept.setHandleDeptName(optPatrolTask.getExecuteOrganizationName());
                optHandleFlowRecordEventAccept.setShouldFinishTime(optPatrolTask.getEndTime());//应完成时间
                optHandleFlowRecordEventAccept.setHandleTimeLimit(optPatrolTask.getEndTime());//处置时限截止时间
                //optHandleFlowRecordEvent.setHandleTime(new Date());
                optHandleFlowRecordEventAccept.setCreateBy(SecurityUtils.getUsername());//当前登录用户姓名
                createDate.setTime(createDate.getTime() + 50);
                optHandleFlowRecordEventAccept.setCreateTime(createDate);
                optHandleFlowRecordEventAccept.setContent("系统自动派遣");
                optHandleFlowRecordService.save(optHandleFlowRecordEventAccept);
            }
            // 如果含有报警记录id 则更新报警表处置状态为处置中 2023-01-05 liqingbo
            if (ObjectUtil.isNotNull(optPatrolTask.getAlarmId())) {
                iOptDeviceAlarmService.updateConfirmAlarmToInDisposal(optPatrolEvent.getAlarmId());
            }
            // 如果含有断面报警记录id 则更新断面报警表处置状态为处置中 2023-01-05 liqingbo
            if (ObjectUtil.isNotNull(optPatrolTask.getMinorId()) && state != null){
                if (state == 0) {//排口
                    smdtvOutletProblemService.updateConfirmAlarmToInDisposal(minorId,1);
                } else if (state == 1) {//溢流井
                    smdtvOverflowWellService.updateConfirmAlarmToInDisposal(minorId,1);
                } else if (state == 2) {
                    exteriorSiteAlarmService.updateConfirmAlarmToInDisposal(minorId,1);
                }
            }
            return isSuccess;
        } catch (Exception ex)  {
            log.error("设备自动报警传入参数对象异常记录：" + JSONObject.toJSONString(optPatrolTask));
            log.error("自动生成设备报警任务并下派任务deviceTaskAdd失败：" + ex.getMessage());
            return false;
        }
    }
}
